$(function() {
    $.initPage(eLancoApprovalList.load.start);
});

var eLancoApprovalList = new BaseFrame();

eLancoApprovalList.addBlock('search', 'list', 'pagination');

//load module
eLancoApprovalList.load.addStart(function() {
    var params = eLancoApprovalList.data.search.params;
    $.initApprovalPage(params)
        .then(function(data) {
            _data = $.extend({}, data);
            return $.pageStart(_eLancoSettings);
        })
        .then(function() {
            eLancoApprovalList.load.runTask();
            eLancoApprovalList.event.runTask();
            eLancoApprovalList.load.list.create(_data.approvalList, true);
        });
});

eLancoApprovalList.load.addBind('createTitle', function() {
    var currentPage = $.getCurrentPage(_eLancoSettings.pageList);
    $('title').text(currentPage.title);
});

eLancoApprovalList.load.addBind('createNav', function() {
    var navList = $.getUserAuthorityList(_user.roleList, _eLancoSettings.authorityList, _eLancoSettings.pageList);
    $('#logout').on('click', $.logoff);
    $('#block-nav').createNavList(navList).removeClass('dn');
});

eLancoApprovalList.load.search.addBind('create', function() {
    if (eLancoApprovalList.verify.isSubmitModel()) return;
    var domStorage = $('#form-search');
    $('#field-search-type').createSelect(domStorage, 'search.formId', $.getKeyValueList('id', 'formChineseName', _data.formList), {
        verify: false,
        firstOption: _allSelect,
    });
    $('#field-search-formNumber').createString(domStorage, 'search.formNumber', {
        verify: false,
    });
    $('#field-search-approvalStatus').createSelect(domStorage, 'search.approvalStatus', $.getKeyValueListByObject(_enumApprovalStatus), {
        verify: false,
        firstOption: _allSelect,
    });
    $('#field-search-requesterEmployeeId').createString(domStorage, 'search.requesterEmployeeId', {
        verify: false,
    });
    $('#field-search-activityNameDescription').createString(domStorage, 'detail.activityNameDescription', {
        verify: false,
    });
    $('#field-search-positionId').createString(domStorage, 'detail.positionId', {
        verify: false,
    });
    $('#field-search-hiringManager').createString(domStorage, 'detail.hiringManager', {
        verify: false,
    });
    $('#field-search-requestDate').createDoubleDatepicker(domStorage, 'detail.requestDateStart, detail.requestDateEnd', {
        verify: false,
        startDatepickerOptions: {
            startDate: null,
        },
        endDatepickerOptions: {
            startDate: null,
            endDate: new Date(),
        },
    });
    $('#block-search .search-item[formId]').hide();
    $('#block-search').removeClass('dn');
});

eLancoApprovalList.load.list.addEvent('create', function(dataList, isFirst) {
    if (dataList.length == 0) {
        $('#block-alert').text(isFirst ? _enumMessage.noResult : _enumMessage.noSearchResult);
        $('#list-list').addClass('dn');
        $('#block-alert').removeClass('dn');
        $('#block-pagination').addClass('dn');
    } else {
        var formId = $('#form-search').data('search').formId;
        var headerTemplate = $('#template-list-header').html();
        var listTemplate = $('#template-list-detail').html();
        if (!$.isEmpty(formId)) {
            headerTemplate = $('#template-list-header-' + formId).html();
            listTemplate = $('#template-list-detail-' + formId).html();
        }
        $('#list-list thead').empty().createTemplate(headerTemplate);
        $('#list-list tbody').empty().createListTemplate(listTemplate, dataList);
        eLancoApprovalList.load.pagination.create(_data.pageTotal);
        $('#list-list').removeClass('dn');
        $('#block-alert').addClass('dn');
    }
    $('#block-list').removeClass('dn');
});

eLancoApprovalList.load.list.addEvent('update', function(pageNumber) {
    var params = eLancoApprovalList.save.search.params();
    params.pageNumber = pageNumber || params.pageNumber;
    $.getApprovalList(params)
        .then(function(data) {
            if (data.pageId != eLancoApprovalList.data.search.params.pageId) return false;
            _data.approvalList = data.approvalList;
            _data.pageTotal = data.pageTotal;
            eLancoApprovalList.load.list.create(_data.approvalList);
        });
});

eLancoApprovalList.load.pagination.addEvent('create', function() {
    if (_data.approvalList.length == 0) return;
    pageNumber = eLancoApprovalList.data.search.params.pageNumber;
    pageSize = eLancoApprovalList.data.search.params.pageSize;
    pageTotal = _data.pageTotal
    $('#block-pagination').createPagination({
        pageSize: pageSize,
        pageNumber: pageNumber,
        pageTotal: pageTotal,
    }).removeClass('dn');
});

//event module
eLancoApprovalList.event.search.addBind('formTypeChange', function() {
    $('#field-search-type').findFormUnit().on('change', function() {
        var thisDom = $(this);
        var value = thisDom.val();
        var formSearchDom = $('#form-search');
        var formId = formSearchDom.data('search').formId;
        formSearchDom.find('.search-item[formId]').fadeOut();
        if (!$.isEmpty(value)) {
            formSearchDom.find('.search-item[formId=' + formId + ']').fadeIn();
        }
    });
});

eLancoApprovalList.event.search.addBind('operation', function() {
    $('#block-search').on('click', '#search-btn', function() {
        eLancoApprovalList.load.list.update(1);
        return false;
    });
});

eLancoApprovalList.event.list.addBind('operation', function() {
    $('#list-list').on('click', '.form-delete', function() {
        $.delete(_apiPath + '/FormContents/' + $(this).data('id'))
            .then(function(data, status) {
                $.alert(_enumMessage.deleteSuccess, eLancoApprovalList.load.list.update);
            });
    });
})

eLancoApprovalList.event.pagination.addBind('operation', function() {
    $('#block-pagination').on('changeCallback', function(event, currentPage, targetPage) {
        eLancoApprovalList.load.list.update(targetPage);
    });
});

//form module

//save module
eLancoApprovalList.save.search.addEvent('params', function(pageNumber) {
    var data = $('#form-search').data();
    var params = eLancoApprovalList.data.search.params;
    pageNumber = pageNumber || params.pageNumber;
    params.pageId++;
    params.pageNumber = pageNumber;
    params = $.extend({}, params, data.search);
    params.detail = $.extend({}, data.detail);
    eLancoApprovalList.data.search.params = params;
    return params;
});

//verify module
eLancoApprovalList.verify.addEvent('isSubmitModel', function() {
    if ($.getParam('model') == 'submit') return true;
    return false;
});

eLancoApprovalList.verify.addEvent('isApproveModel', function() {
    if ($.getParam('model') == 'approve') return true;
    return false;
});

//message module

//data module
eLancoApprovalList.data.search.addConfig('params', {
    pageId: 0,
    pageNumber: 1,
    pageSize: 5,
});